#!/bin/bash

readonly ISO_REPO_DEFINITION="[isorepo]\nSigLevel = Optional TrustAll\nServer   = file:///isorepo/"
readonly NETINSTALL_MSG="To install Parabola, the system must be connected to the internet."
readonly COMPLETE_MSG="This ISO is capable of installing a complete Parabola system\n\
>>>>without a connection to the internet. If you would like to fetch\n\
>>>>the latest packages from the internet, run the following command\n\
>>>>before beginning the install:\n\
        # cp /etc/pacman-online.conf /etc/pacman.conf"
[ "${enable_offline_install}" == 'false' ] && readonly NETWORK_MSG=${NETINSTALL_MSG} || \
                                              readonly NETWORK_MSG=${COMPLETE_MSG}


set -e -u

sed -i 's/#\(en_US\.UTF-8\)/\1/' /etc/locale.gen
locale-gen

ln -sf /usr/share/zoneinfo/UTC /etc/localtime

usermod -s /usr/bin/zsh root 2> /dev/null
cp -aT /etc/skel/ /root/

[ -n "$(id parabola 2> /dev/null)" ] && userdel -r parabola 2> /dev/null
useradd -m -p "" -g users -G "adm,audio,floppy,log,network,rfkill,scanner,storage,optical,power,wheel" -s /usr/bin/zsh parabola

chmod 750 /etc/sudoers.d
chmod 440 /etc/sudoers.d/g_wheel

sed -i "s/#Server/Server/g" /etc/pacman.d/mirrorlist
if   [ "${enable_offline_install}" == 'true' ]
then echo -e "${ISO_REPO_DEFINITION}" >> /etc/pacman.conf
     mv /etc/pacman.conf         /etc/pacman-online.conf
     cp /etc/pacman-offline.conf /etc/pacman.conf
else rm /etc/pacman-offline.conf
fi

systemctl enable multi-user.target pacman-init.service choose-mirror.service

sed -i "s|_EDITION_TITLE_|${iso_title}|"                          /etc/motd
sed -i "s|_NETWORK_MSG_|${NETWORK_MSG}| ; s|^>>>>\(.*\)|    \1|g" /etc/motd
